package ge.telasi.tasks.ui.search;

import ge.telasi.tasks.search.Condition;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;

/**
 * Single search line.
 *
 * @author dimitri
 */
public class SearchLine extends JPanel {
    static Properties TYPE_EDITORS;
    static {
        try {
            TYPE_EDITORS = new Properties();
            TYPE_EDITORS.load(SearchLine.class.getResourceAsStream("search.properties"));
        } catch (Exception ex) {}
    }

    private JComboBox combo;
    private FieldEditor editor;
    private SearchPanel searchPanel;

    public SearchLine() {
        initComponents();
        setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
    }

    private void initComponents() {
        setLayout(new BorderLayout(5, 5));
        // field combo
        combo = new JComboBox();
        combo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                displayEditor();
            }
        });
        combo.setRenderer(new FieldRenderer());
        add(combo, BorderLayout.WEST);
        // delete button
        JButton button = new JButton();
        button.setIcon(UIUtils.getIcon16x16("delete.png"));
        Dimension size = button.getPreferredSize();
        button.setPreferredSize(new Dimension(25, size.height));
        button.setBorderPainted(false);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                searchPanel.deleteLine(SearchLine.this);
            }
        });
        add(button, BorderLayout.EAST);
    }

    public FieldEditor getEditor() {
        return editor;
    }

    public SearchPanel getSearchPanel() {
        return searchPanel;
    }

    public void setSearchPanel(SearchPanel searchPanel) {
        this.searchPanel = searchPanel;
    }

    public void setFields(Field[] fields) {
        combo.setModel(new DefaultComboBoxModel(fields));
        displayEditor();
    }

    private void displayEditor() {
        if (editor != null) {
            remove((Component) editor);
        }
        Field selected = (Field) combo.getSelectedItem();
        if (selected != null) {
            try {
                String type = selected.getType();
                String classname = TYPE_EDITORS.getProperty(type);
                Class cl = Class.forName(classname);
                editor = (FieldEditor) cl.newInstance();
                Component editorComponent = (Component) editor;
                editor.setField(selected);
                add(editorComponent, BorderLayout.CENTER);
            } catch (Exception ex) {
                LoggerUtils.manageException(this, ex);
            }
        }
        revalidate();
        doLayout();
    }

    public Condition getCondition() {
        return editor.getField().getCondition();
    }

    private class FieldRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Field) {
                Field field = (Field) value;
                setIcon(field.getIcon());
            }
            return this;
        }
    }

}
